package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.TbOrder;
import com.example.demo.mapper.UserMapper;
import com.example.demo.util.ResultMsg;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, TbOrder> implements UserService {
    //        this.saveOrUpdate() 新增或修改
//        this.remove() 根据条件删除
//        this.list() 根据条件进行查询所有
//        this.page() 分页查询
    @Override
    public ResultMsg show() {
        List<TbOrder> list = this.list((Wrapper<TbOrder>) null);
        return ResultMsg.SUCCEESS(200,null,list);
    }

    @Override
    public ResultMsg show(String key) {
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.like("order_name",key);
        List<TbOrder> list = this.list(wrapper);
        return ResultMsg.SUCCEESS(200,null,list);
    }

    @Override
    public ResultMsg show(int current, int size) {
        Page<TbOrder> ipage = new Page<>(current,size);
        Page<TbOrder> page = this.page(ipage);
        return ResultMsg.SUCCEESS(200,null,page);
    }

    @Override
    public ResultMsg modify(TbOrder tbOrder) {
        boolean res=this.saveOrUpdate(tbOrder);
        System.out.println("res = " + res);
        if(res)
            return ResultMsg.SUCCEESS(200,null,null);
        return ResultMsg.FAIL(400,null);
    }

    @Override
    public ResultMsg deleteById(String id) {
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        boolean remove = this.remove(wrapper);
        return ResultMsg.SUCCEESS(200,null,null);
    }
}
